/**
 * 
 */
package Exercise;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author zhang //分组分数 最大
 */
public class ZTEPower {

	public static void main(String[] args) {
		int[][] pair = { { 1, 2 }, { 2, 3 }, { 7, 8 }, { 5, 6 }, { 7, 9 }, { 4, 5 } };
		int[] stength = { 25, 34, 23, 45, 16, 51, 29, 38, 47 };
		long calculatPower = calculatPower(9, 6, pair, stength);
		System.out.println(calculatPower);
	}

	public static long calculatPower(int numOfKids, int cardDrawn, int[][] pair, int[] strength) {
		Map<Integer, Set<Integer>> map = new HashMap<>();
		for (int i = 0; i < pair.length; i++) {
			if (!map.containsKey(pair[i][0])) {
				HashSet<Integer> list = new HashSet<>();
				list.add(pair[i][0]);
				list.add(pair[i][1]);
				map.put(pair[i][0], list);
			} else {
				map.get(pair[i][0]).add(pair[i][1]);
			}
		}
		for (int i = 0; i < pair.length; ++i) {
			for (int j = 0; j < pair.length; j++) {
				if (pair[i][0] == pair[j][1]) {
					if (i >= j) {
						map.get(pair[j][0]).addAll(map.get(pair[i][0]));
					} else {
						map.get(pair[i][0]).addAll(map.get(pair[j][0]));
					}
				}
			}
		}

		Iterator<Integer> iterator = map.keySet().iterator();
		long max = 0L;
		while (iterator.hasNext()) {
			Set<Integer> set = map.get(iterator.next());
			System.out.println(set.size());
			long sum = 0;
			for (Integer i : set) {
				sum += strength[i - 1];
			}
			if (sum > max) {
				max = sum;
			}
		}
		return max;
	}

}
